// pages/traincar/orderdetail.js
var util = require('../../utils/util.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showTopTips: false,
        errorMsg: "",
        bannerList: [],
        activeBannerIndex: 1,
        orderid: 0,
        orderinfo: [],
        isshow: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            orderid: options.oid
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getOrderdetail();
        this.initBanner();
    },
    //广告图片初始化
    initBanner: function () {
        var that = this;
        var requrl = app.globalData.hostUrl + 'common/banner';
        var imgbanner = wx.getStorageSync('ycBanner');
        //console.log(imgbanner.length);
        if (!app.expiratTime('ycBanner') || imgbanner.length == 0) {
            wx.request({
                url: requrl,
                data: { 'location': '303' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        wx.setStorageSync('ycBanner', res.data.result);
                        that.setData({
                            bannerList: res.data.result
                        })
                    }
                }
            })
        }
        else {
            that.setData({
                bannerList: imgbanner
            })
        }
    },
    getOrderdetail: function () {
        var that = this;
        var requrl = app.globalData.hostUrl + 'handler/orderinfo';
        wx.request({
            url: requrl,
            data: { 'oid': that.data.orderid },
            header: {
                'grant-appid': app.globalData.appid
            },
            success: function (res) {
//                console.log(res);
                if (res.data.code == 200) {
                    var orderinfo = res.data.result;
                    var _isshow = true;
                    if (orderinfo.status != 0) {
                        _isshow = false;
                    }
                    that.setData({
                        orderinfo: orderinfo,
                        isshow: _isshow
                    })
                }
            }
        })
    },
    confirmpay: function () {
        var that = this;
        var clickflag = 1;
        var postdata = {
            'openid': wx.getStorageSync('openId'),
            'uid': wx.getStorageSync('userId'),
            'oid': this.data.orderid,
            'flag': 'lc'
        };
        var requrl = app.globalData.hostUrl + 'handler/orderPrepay';
        if (clickflag == 1) {
            clickflag = 0;
            wx.request({
                url: requrl,
                data: postdata,
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'grant-appid': app.globalData.appid
                },
                method: 'POST',
                success: function (res) {
                   // console.log(res);
                    if (res.data.code == 200) {
                     // console.log('调起支付');
                      wx.requestPayment({
                        'timeStamp': res.data.result.timeStamp,
                        'nonceStr': res.data.result.nonceStr,
                        'package': res.data.result.package,
                        'signType': 'MD5',
                        'paySign': res.data.result.paySign,
                        'success': function (res) {
                          wx.showModal({
                            title: '提示',
                            content: '支付成功!',
                            success: function (res) {
                              wx.navigateTo({
                                url: '../myself/myappointment',
                              })
                            }
                          });
                        },
                        'fail': function (res) {

                        },
                        'complete': function (res) {
                          wx.navigateTo({
                            url: '../myself/myappointment',
                          })
                        }
                      });
                       
                    }
                    else {
                        util.isError(res.data.result, that);
                    }
                    clickflag = 1;
                }
            })
        }
    }

})